/**
 * elancom Corporation copy right 2012 - 2015  
 *   
 * $Header: https://192.168.1.224/svn/elancom/product/elancom-platform/trunk/src/main/java/com/elancom/framework/tags/ComponentTagSupport.java 1034 2012-07-02 03:09:12Z lanzhouxue $
 * $Id: ComponentTagSupport.java 1034 2012-07-02 03:09:12Z lanzhouxue $
 * $Author: lanzhouxue $
 * $Date: 2012-07-02 11:09:12 +0800 (周一, 02 七月 2012) $
 * $Revision: 1034 $
 */
package com.elancom.framework.web.tags;

import javax.servlet.jsp.PageContext;

import com.elancom.framework.component.ComponentInvoker;
import com.elancom.framework.component.ComponentRepository;
import com.elancom.framework.component.Input;
import com.elancom.framework.component.InputFactory;

/**
 * @author elancom
 * @date 2012-5-6
 */
@SuppressWarnings("serial")
public abstract class ComponentSupportTag extends BeanSupportTag {
	protected ComponentRepository componentRepository;

	protected ComponentInvoker componentInvoker;

	private InputFactory inputFactory;

	@Override
	public void setPageContext(PageContext pageContext) {
		super.setPageContext(pageContext);
		initTag();
	}

	protected void initTag() {
		getComponentRepository(pageContext);
		getComponentInvoker();
		getInputFactory();
	}

	protected ComponentInvoker getComponentInvoker() {
		if (componentInvoker != null) {
			return componentInvoker;
		}

		if (pageContext == null) {
			throw new RuntimeException("pageContext can't be null.");
		}

		componentInvoker = getBean("componentInvoker");
		return componentInvoker;

	}

	protected ComponentRepository getComponentRepository(PageContext pageContext) {
		if (componentRepository != null) {
			return componentRepository;
		}

		if (pageContext == null) {
			throw new RuntimeException("pageContext can't be null.");
		}

		componentRepository = getBean("componentRepository");
		return componentRepository;
	}

	/**
	 * @return the inputFactory
	 */
	public InputFactory getInputFactory() {
		if (inputFactory == null) {
			inputFactory = getBean("inputFactory");
		}
		return inputFactory;
	}

	/**
	 * @param inputFactory
	 *            the inputFactory to set
	 */
	public void setInputFactory(InputFactory inputFactory) {
		this.inputFactory = inputFactory;
	}

	protected Input createInput() {
		return inputFactory.create();
	}
}
